﻿
'*******************************************************************
' Program: DC_Rack
' Author: Albert E Edlund
' Date:
'
' Purpose: provides a report viewer control for working with
' datatables
'
'*********************************************************************


' basic system calls
Imports System
Imports System.Windows.Forms
Imports System.Text
Imports System.Data
Imports System.Threading
Imports System.Timers
Imports System.Drawing
Imports System.Drawing.Image
Imports System.Drawing.Imaging
Imports System.Drawing.Printing
Imports System.Runtime.InteropServices

Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization

Imports Microsoft.Reporting
Imports Microsoft.Reporting.WinForms
Imports Microsoft.ReportingServices
Imports Microsoft.ReportingServices.ReportRendering

Imports visRackSysMgmt.clsDC_RackReporting



' the exception handling and logging
Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging



Public Class frmReportViewer


#Region " Properties and declarations "


    Private m_clsReporting As clsDC_RackReporting
    Private Property _clsReporting As clsDC_RackReporting

        Get
            Return Me.m_clsReporting
        End Get
        Set(ByVal value As clsDC_RackReporting)
            Me.m_clsReporting = value
        End Set

    End Property



#End Region

#Region " Report Processing "


    Private Sub frmReportViewer_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated

        Try

            If _clsReporting IsNot Nothing Then
                _clsReporting._LocalViewer.RefreshReport()
            End If

        Catch rpt As ReportViewerException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(rpt, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    Private Sub frmReportViewer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.ReportViewer1.RefreshReport()

    End Sub



#End Region

#Region " constructor "

    '
    ' pass in the name of the report and the datatable
    '
    Public Sub New _
        (ByVal intReport As clsDC_RackReporting.baseReports, _
         ByRef dtTempTable As System.Data.DataTable)

        Try


            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.

            ReportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local

            If _clsReporting IsNot Nothing Then
                _clsReporting = Nothing
                _clsReporting = New clsDC_RackReporting(Me.ReportViewer1, intReport, dtTempTable)
            Else
                _clsReporting = New clsDC_RackReporting(Me.ReportViewer1, intReport, dtTempTable)
            End If

        Catch rpt As ReportViewerException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(rpt, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
    End Sub


#End Region

#Region " form events "

    '
    '
    '
    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Me.Close()
    End Sub

    '
    '
    '
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub


    '
    ' dump the report out as an excel file
    '
    Private Sub ExcelToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExcelToolStripMenuItem.Click

        Try

            _clsReporting.createLocalReport()

            Dim bytes As Byte() = _clsReporting.RenderReportStandard("Excel")

            If Not (bytes Is Nothing) Then
                Dim saveFileDialog1 As New SaveFileDialog()
                saveFileDialog1.Filter = "excel files (*.xls)|*.xls|All files (*.*)|*.*"
                saveFileDialog1.FilterIndex = 1
                saveFileDialog1.RestoreDirectory = True

                Dim strFileName As String = ""
                Dim strPathName As String = ""
                If saveFileDialog1.ShowDialog() = DialogResult.OK Then
                    strFileName = saveFileDialog1.FileName
                End If
                Dim fs As New FileStream( _
                            strFileName, _
                            FileMode.Create)

                fs.Write(bytes, 0, bytes.Length)
                fs.Close()
            End If

        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub

    '
    ' dump the report out as a pdf file
    '
    Private Sub PDFToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PDFToolStripMenuItem.Click


        Try

            _clsReporting.createLocalReport()
            Dim bytes As Byte() = _clsReporting.RenderReportStandard("PDF")

            If Not (bytes Is Nothing) Then
                Dim saveFileDialog1 As New SaveFileDialog()
                saveFileDialog1.Filter = "pdf files (*.pdf)|*.pdf|All files (*.*)|*.*"
                saveFileDialog1.FilterIndex = 1
                saveFileDialog1.RestoreDirectory = True

                Dim strFileName As String = ""
                Dim strPathName As String = ""
                If saveFileDialog1.ShowDialog() = DialogResult.OK Then
                    strFileName = saveFileDialog1.FileName
                End If
                Dim fs As New FileStream( _
                            strFileName, _
                            FileMode.Create)

                fs.Write(bytes, 0, bytes.Length)
                fs.Close()
            End If

        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

 

    End Sub


    '
    ' dump the report as a text (csv) file
    '
    Private Sub TextToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextToolStripMenuItem.Click


        Try


            _clsReporting.createLocalReport()

            Dim strReport As String = _clsReporting.RenderReportText()

            Dim saveFileDialog1 As New SaveFileDialog()
            saveFileDialog1.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*"
            saveFileDialog1.FilterIndex = 1
            saveFileDialog1.RestoreDirectory = True

            Dim strFileName As String = ""
            Dim strPathName As String = ""
            If saveFileDialog1.ShowDialog() = DialogResult.OK Then
                strFileName = saveFileDialog1.FileName
            End If

            If File.Exists(strFileName) = False Then
                File.WriteAllText(strFileName, strReport)
            End If


        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub


    '
    ' dump the report as an image
    '
    Private Sub ImageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImageToolStripMenuItem.Click


        Try

            _clsReporting.createLocalReport()

            Dim bytes As Byte() = _clsReporting.RenderReportMetafileAsByte()
            If Not (bytes Is Nothing) Then
                Dim saveFileDialog1 As New SaveFileDialog()
                saveFileDialog1.Filter = "emf files (*.emf)|*.emf|All files (*.*)|*.*"
                saveFileDialog1.FilterIndex = 1
                saveFileDialog1.RestoreDirectory = True

                Dim strFileName As String = ""
                Dim strPathName As String = ""
                If saveFileDialog1.ShowDialog() = DialogResult.OK Then
                    strFileName = saveFileDialog1.FileName
                End If
                Dim fs As New FileStream( _
                            strFileName, _
                            FileMode.Create)

                fs.Write(bytes, 0, bytes.Length)
                fs.Close()
            End If

        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub



#End Region


End Class